package br.inf.ufrgs.br.usixml4cdc.rendering;

import java.io.File;
import java.io.IOException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.xerces.parsers.DOMParser;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.SAXException;

public class ResourceRetriever {
	
	private Element resourceModel = null;
	
	public ResourceRetriever(Element resourceModel) {
		this.resourceModel = resourceModel;
	}
	
	public String getStringResourceContent(Element element){
           
            String cioId = element.getAttribute("id");

            int numChildren = this.resourceModel.getChildNodes().getLength();
		for (int i = 0; i < numChildren; i++) {
                    if ( this.resourceModel.getChildNodes().item(i) instanceof Element){
                        Element elem = (Element) this.resourceModel.getChildNodes().item(i);
                        String id = elem.getAttribute("cioId");
                        if (id.equals(cioId)){
                            return this.getContent(elem);
                        }
                    }
                }
                    
            return "";
	}

    private String getContent(Element cioRef) {
        int numChildren = cioRef.getChildNodes().getLength();
        for (int i = 0; i < numChildren; i++) {
            if (cioRef.getChildNodes().item(i) instanceof Element){
                Element resourceElement = (Element)cioRef.getChildNodes().item(i);
                return resourceElement.getAttribute("content");
            }
        }
        
        return "";
    }

}
